package cn.iocoder.yudao.module.digital.service.cardchannel.impl.bh.util;

import cn.hutool.http.HttpUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;

import java.util.List;
import java.util.Map;

/**
 * Description:
 * Author: li
 * Date: 2024/3/5
 */
public class CardKeyThirdPartyApiUtil {

    /**
     * 生成请求人Id
     *
     * @param value
     * @return
     */
    public static String generateRequestorId(Long value) {
        if (value == null) {
            throw new IllegalArgumentException("Value cannot be null");
        }

        String stringValue = String.valueOf(value);
        int length = stringValue.length();

        // 确保字符串的长度不超过26位
        if (length > 26) {
            throw new IllegalArgumentException("Value is too large to convert to requestorId");
        } else {
            // 不足12位则在前面补零
            StringBuilder zeros = new StringBuilder();
            for (int i = 0; i < 26 - length; i++) {
                zeros.append("0");
            }
            stringValue = zeros.toString() + stringValue;
        }

        return stringValue;
    }

    /**
     * 生成交易的检索参考号
     *
     * @param value
     * @return
     */
    public static String generateRetrievalReferenceNumber(Long value) {
        if (value == null) {
            throw new IllegalArgumentException("Value cannot be null");
        }

        String stringValue = String.valueOf(value);
        int length = stringValue.length();

        // 确保字符串的长度不超过11位
        if (length > 11) {
            throw new IllegalArgumentException("Value is too large to convert to retrievalReferenceNumber");
        } else {
            // 不足12位则在前面补零
            StringBuilder zeros = new StringBuilder();
            for (int i = 0; i < 12 - length; i++) {
                zeros.append("0");
            }
            stringValue = zeros.toString() + stringValue;
        }

        return stringValue;
    }

    public static void main(String[] args) {
        String urlTemplate = "https://giftz.com.au/products/json?publisherId=91&cities=137&page=%s&sort=&itemsPerPageDropDown=12&categoriesIds=&isBrandFlow=false";

        int page = 0;
        while (true) {
            page++;
            String url = String.format(urlTemplate, page);
            String jsonString = HttpUtil.get(url);
            Map map = JsonUtils.parseObject(jsonString, Map.class);
            List<Map> contentList = (List<Map>) map.get("content");
            if (CollectionUtils.isAnyEmpty(contentList)) {
                break;
            }

            for (Map obj : contentList) {
                String title = (String) obj.get("title");
                System.out.println(title);
            }
        }

    }

}
